Add app.ready() and Model ready hooks - #532
Conversation
857cad8 to
ff6652c
Compare
|
@bajtos where in |
|
It might also make sense to add
|
|
@ritch does Model has a |
Nope |
|
@ritch Can you list a few examples of what is possible in the |
|
@ritch The |
This is a very bad example:
This is the corrected version: |
See |
There was a problem hiding this comment.
Please fix this per my comment above.
|
I am seconding @clarkorz @kraman and would like to know what are the use cases for these new hooks? We should be careful to not introduce a feature that would be confusing to loopback users and/or make it easy to use it incorrectly (e.g. set up remoting too late). Another thing to consider is inconsistency between |
|
@bajtos I want to hide some of the shared methods. So I'm seeking an event/hook/etc. to do such thing. (FYI, Currently I set some flags in model's JSON file and create a boot script related issue:
related PR: The PR works in following conditions:
Currently, loopback emits So I thought, I need an event or hook that a model has been fully configured. But after discuss with @raymondfeng in that PR, I realize that this is a false proposition, because we could add relations and scopes even after loopback bootstrapped (maybe remove and alter in the future?). That's why I propose an |
|
I think The main use case for this is for the explorer, angular sdk, and anything that wants reads metadata defined at runtime. Eg. a script that lists all available remote methods... function listRemoteMethods(app) {
app.models().forEach(function(model) {
model.sharedClass.methods().forEach(function(sharedMethod) {
console.log(sharedMethod.name);
});
});
}The issue we keep running into is... when can I run setTimeout(function() {
app.models.MyModel.remoteMethod(...);
}, ...);This method will not be listed, and it won't be clear why. Sure the example is contrived, but there is nothing in loopback that makes it clear that you cannot do this. Adding a Here's the fix to the issue mentioned above with the proposed MyModel.define = function(cb) {
setTimeout(function() {
MyModel.remoteMethod(...);
cb();
}, 1);
}
app.on('defined', function() { listRemoteMethods() }); |
|
Ran into this exact issue when working on adding relations to the remote connector yesterday. How about being a bit more prescriptive about the lifecycle stages. Something like the stages below (you will need to define the actual stages since I am not very familiar with loopback model initialization).
|
|
@kraman I agree. We need to define the lifecycle phases and corresponding hooks/events so that developers can understand when to do what. |
In some cases, the correct answer is "never". Models can be added and modified even after the application started, there was at least one user asking for that feature. Therefore the event name "frozen" is misleading. From the POV of strong-remoting and swagger/explorer, a better solution is to provide From the POV of angular code generator, it still makes sense to have an app-wide event fired when the models available at boot time are fully configured. (Related: strongloop/grunt-loopback-sdk-angular#5) |
|
This was a dead end... Closing for now. |
@bajtos @raymondfeng @fabien @kraman
This adds a set of ready hooks to
Modelclasses. Override these hooks to implement custom behavior that depends on your app being bootstrapped:loopback-bootcompleteDocs
Do not call this method if you are using
loopback-bootto bootstrap your application.loopback-bootwill call this method for you! Otherwise you must callapp.ready()to runthe
Model.ready()hooks.Calling
ready()will callready()on all models attached to theapp. Overridethe
ready()method on aModelclass to ensure you have access to a bootstrapped application.